var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    array: ['请选择反馈类型', '留言', '投诉', '询问', '售后', '求购'],
    index: 0,
    userimg:'',
    user_thumb:''
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  formSubmit:function(e){
    var that = this;
    let user_id = wx.getStorageSync('userId');
    let userInfo = wx.getStorageSync('userInfo');
    let user_thumb = that.data.user_thumb;
    if(e.detail.value.msg_type==0){
      util.showErrorToast('请选择反馈类型');
      return false;
    }

    var strkong = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!strkong.test(e.detail.value.msg_title)) {
      util.showErrorToast('请填写正确的手机电话');
      return false;
    }

    let msgdata = { user_id: user_id, user_name: userInfo.user_name, email: userInfo.email, msg_title: e.detail.value.msg_title, msg_content: e.detail.value.msg_content, msg_type: e.detail.value.msg_type, user_thumb:user_thumb};
    util.request(api.AddMessage, msgdata).then(function (res) {
      if (res.errno == 0) {
        wx.showToast({
          title: res.data,
          icon: 'success',
          duration: 2000
        })
        wx.redirectTo({
          url: '/pages/ucenter/index/index',
        })
      }
    });

  },
  bindChangeImg: function () {
    var that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        wx.uploadFile({
          url: api.UploadImg,
          filePath: tempFilePaths[0],
          name: 'user_thumb',
          formData: {
            'imgIndex': 0, 
            has_code: api.hasCode,
            uptype: 2
          },
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            if (data.errno == 0) {
              let image = api.Domain + data.data;
              that.setData({
                userimg: image,
                user_thumb: data.data
              })
            }

          },
          fail: function (res) {
            wx.hideToast();
            util.showErrorToast('上传图片失败');
            return false;
          }
        });
      }
    });
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "意见反馈"
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  }
})